/*
 * OptionsTag.java
 *
 * Created on 2007年7月19日, 下午5:34
 *
 * 花开堪折须直折，莫待无花空折枝！
 *
 */

package com.ecomm.util.taglibs;
import com.ecomm.util.EcomTagLib;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.struts.taglib.TagUtils;
import org.apache.struts.taglib.html.Constants;
import org.apache.struts.taglib.html.OptionTag;
/**
 *
 * <p>Title: 易讯聊天系统</p>
 * <p>Description:
 *  易讯聊天系统
 * </p>
 * <p>Copyright: Copyright (c) 2007</p>
 * <p>Company: 联合易讯 </p>
 * @author <font color='blue'>ppliang</font> &nbsp&nbsp<a href="mailto:pijunliang@hotmail.com">pijunliang@hotmail.com</a>
 * @version 1.0
 * @data  2007年7月19日, 下午5:34
 */
public class OptionsTag extends OptionTag {
  private String selected = null;
  private String value = null;
  private String selectValue = null;
  /**
   * Process the start of this tag.
   *
   * @exception JspException if a JSP exception has occurred
   */
  public int doStartTag() throws JspException {
    
    // Initialize the placeholder for our body content
    this.text = null;
    
    // Do nothing until doEndTag() is called
    return (EVAL_BODY_TAG);
    
  }
  
  /**
   * Process the body text of this tag (if any).
   *
   * @exception JspException if a JSP exception has occurred
   */
  public int doAfterBody() throws JspException {
    
    if (bodyContent != null) {
      String text = bodyContent.getString();
      if (text != null) {
        text = text.trim();
        if (text.length() > 0) {
          this.text = text;
        }
      }
    }
    return (SKIP_BODY);
    
  }
  
  /**
   * Process the end of this tag.
   *
   * @exception JspException if a JSP exception has occurred
   */
  public int doEndTag() throws JspException {
    HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
    HttpServletResponse response=(HttpServletResponse)pageContext.getResponse();
    selectValue = (String)request.getAttribute("selectTag.value");
    String selectTagname = (String)request.getAttribute("selectTag.name");
    String selectTagpermission = (String)request.getAttribute("selectTag.permission");
    EcomTagLib stl = (EcomTagLib)request.getAttribute("ecomTaglib");
    if(!stl.getlogic(selectTagname,selectTagpermission)){
      TagUtils.getInstance().write(pageContext, this.renderOptionElement());
    }else if(selectValue != null && selectValue.equals(this.getValue())){
      TagUtils.getInstance().write(pageContext, text());
    }
    
    return (EVAL_PAGE);
  }
  
  /**
   * Generate an HTML %lt;option&gt; element.
   * @throws JspException
   * @since Struts 1.1
   */
  protected String renderOptionElement() throws JspException {
    StringBuffer results = new StringBuffer("<option value=\"");
    
    results.append(this.getValue());
    results.append("\"");
    if (disabled) {
      results.append(" disabled=\"disabled\"");
    }
    if (selectValue != null && selectValue.equals(this.getValue())) {
      results.append(" selected=\"selected\"");
    }else if(getSelected()!= null && getSelected().equals("selected")){
      
      results.append(" selected=\"selected\"");
    }
    results.append(">");
    
    results.append(text());
    
    results.append("</option>");
    return results.toString();
  }
  
  // ------------------------------------------------------ Protected Methods
  
  /**
   * Return the text to be displayed to the user for this option (if any).
   *
   * @exception JspException if an error occurs
   */
  protected String text() throws JspException {
    String optionText = this.text;
    
    if ((optionText == null) && (this.key != null)) {
      optionText =
          TagUtils.getInstance().message(pageContext, bundle, locale, key);
    }
    
    // no body text and no key to lookup so display the value
    if (optionText == null) {
      optionText = this.getValue();
    }
    
    return optionText;
  }
  
  
  public String getValue() {
    return value;
  }
  
  public void setValue(String value) {
    this.value = value;
  }
  
  public String getSelected() {
    return selected;
  }
  
  public void setSelected(String selected) {
    this.selected = selected;
  }
}
